package lex.proxyverfier.actions;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import org.lex.io.IO;
import org.lex.swing.ActionBuilder;
import org.lex.utils.SystemProperties;

import lex.proxyverfier.CK;
import lex.proxyverfier.ProxyVerifier;

public abstract class BaseImportExportAction extends ActionBuilder {
	protected FileFilter fileFilter = null;

	public BaseImportExportAction() {
		super();
	}

	protected abstract FileFilter getFileFilter();

	protected JFileChooser createFileChooser() {
		JFileChooser fc = new JFileChooser(this.getWorkingDirectory());
		if (null == this.fileFilter)
			this.fileFilter = this.getFileFilter();
		fc.addChoosableFileFilter(this.fileFilter);
		fc.setAcceptAllFileFilterUsed(false);
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fc.setMultiSelectionEnabled(false);
		return fc;
	}

	protected File getWorkingDirectory() {
		String dir = ProxyVerifier.getApplication().getContext().getResourceManager().getResourceMap().getString(CK.WORKING_DIRECTORY);
		if (null == dir || dir.isEmpty())
			dir = SystemProperties.userHome();
		return new File(dir);
	}

	protected void setWorkingDirectory(File dir) {
		ProxyVerifier.getApplication().getContext().getResourceManager().getResourceMap().putString(CK.WORKING_DIRECTORY, IO.getCanonicalPath(dir));
	}

	protected void showError(String title, String message) {
		JOptionPane.showMessageDialog(ProxyVerifier.getApplication().getRootWindow(), message, title, JOptionPane.ERROR_MESSAGE);
	}
}
